package csce315.group10.androidapp;

import csce315.group10.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class ContactEntry extends Activity{
	/* private instance variables */
	CustomApp appState;
	Contact chosen_contact;
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contactentry);
        appState = ((CustomApp)getApplicationContext());
        /* set all fields to Contact data */
        chosen_contact = getIntent().getParcelableExtra("contact");
        TextView name = (TextView) findViewById(R.id.Name_text);
        name.setText(chosen_contact.get_name());
        TextView phone = (TextView) findViewById(R.id.Phone_text);
        phone.setText(chosen_contact.get_phone());
        TextView address = (TextView) findViewById(R.id.Address_text);
        address.setText(chosen_contact.get_address());
        TextView email = (TextView) findViewById(R.id.Email_text);
        email.setText(chosen_contact.get_email());
        TextView position = (TextView) findViewById(R.id.Position_text);
        position.setText(chosen_contact.get_position());
    }
    
    /* create menu */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.contact_entry_menu, menu);
        return true;
    }
    
    /* set actions for when menu items are selected */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.edit_contact:     editContact();
            							break;
        }
        return true;
    }
    
    /* start the dialog to edit contact info */
    private void editContact(){
    	Intent intent = new Intent();
    	intent.putExtra("contact",chosen_contact);
    	if(appState.get_current_user().get_position().equals("Editor And Chief")){
    		intent.setClass(this,EditContactDialog.class);
    		startActivityForResult(intent, 0);
    	}else{
    		intent.setClass(this,LimitedEditContactDialog.class);
    		startActivityForResult(intent, 1);
    	} 	
    }
    @Override
	public void onActivityResult(int requestCode,int resultCode, Intent data){
    	/* when prompt for new contact returns */
    	/* if back button was pressed then return */
    	if(resultCode != Activity.RESULT_OK){
    		return;
    	}
      if(requestCode == 0){
    	  /* if returning from an Editor and Chief edit */
    	  chosen_contact.set_phone(data.getStringExtra("phone"));
    	  chosen_contact.set_address(data.getStringExtra("address"));
    	  chosen_contact.set_email(data.getStringExtra("email"));
    	  chosen_contact.set_position(data.getStringExtra("position"));
    	  
      }else if(requestCode == 1){
    	  /* if returning from a non Editor and Chief edit */
    	  chosen_contact.set_phone(data.getStringExtra("phone"));
    	  chosen_contact.set_address(data.getStringExtra("address"));
    	  chosen_contact.set_email(data.getStringExtra("email"));
      }
   }
}